import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: LeetCode
 * @description: LeetCode : 1365. 有多少小于当前数字的数字
 * @author: WXY
 * @create: 2023-02-06 19:46
 * @Version 1.0
 **/
public class Num1365_smallerNumbersThanCurrent {
    public static int[] smallerNumbersThanCurrent1(int[] nums) {
        int[] ans = new int[nums.length];
        int index = 0;
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < nums.length; j++) {
                if (nums[j] < nums[i]) {
                    ans[i]++;
                }
            }
        }
        return ans;
    }
    public static int[] smallerNumbersThanCurrent(int[] nums) {
        int len = nums.length;
        int[] ans = Arrays.copyOf(nums, len);
        Arrays.sort(nums);
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < len; i++) {
            if (map.containsKey(nums[i]) == false) {
                map.put(nums[i], i);
            }
        }
        for (int i = 0; i < len; i++) {
            ans[i] = map.get(ans[i]);
        }
        return ans;
    }

    public static void main(String[] args) {
        int[] arr = {8, 1, 2, 2, 3};
        int[] ans = smallerNumbersThanCurrent(arr);
        for (int num : ans) {
            System.out.print(num + " ");
        }

    }
}
